package org.jay.angla.chart;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.CombinedXYChart.XYCombinedChartDef;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.ScatterChart;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.jay.angla.onlab.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class QAChart extends AbstractDemoChart {
	private List<double[]> x;
	private List<double[]> values;

	public QAChart(Context context) {
		x = new ArrayList<double[]>();
		values = new ArrayList<double[]>();
	}

	public void clear() {
		x.clear();
		values.clear();
	}

	public void drawByFormalu(float k0, float k1, float k2, float k3) {
		double[] xx = new double[100];
		double[] yy = new double[100];
		int index = 0;
		for (float i = 0; i < 4.0f; i = i + 0.04f) {
			double val = k0 + k1 * i + k2 * i * i + k3 * i * i * i;
			if (index >= 100)
				break;
			xx[index] = i;
			yy[index] = val;

			index = index + 1;
		}
		x.add(xx);
		values.add(yy);
	}

	public void drawPoint(double[] xx, double[] yy) {
		x.add(xx);
		values.add(yy);
	}

	public void setYTitle(String title) {
	}

	public GraphicalView getChartView(Context context) {
		String[] titles = new String[] { "Crete Air Temperature",
				"Skiathos Air Temperature" };
		int[] colors = new int[] { Color.GRAY, Color.GREEN };
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,
				PointStyle.DIAMOND };

		while (x.size() < 2) {
			x.add(new double[] { 0 });
			values.add(new double[] { 0 });
		}

		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
		renderer.setPointSize(5.5f);
		int length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = (XYSeriesRenderer) renderer
					.getSeriesRendererAt(i);
			r.setLineWidth(5);
			r.setFillPoints(true);
		}
		renderer.setApplyBackgroundColor(true);
		renderer.setMarginsColor(Color.argb(0, 0xff, 0xff, 0xff));
		renderer.setChartTitleTextSize(30);
		renderer.setAxisTitleTextSize(25);
		renderer.setLegendTextSize(20);
		renderer.setMargins(new int[] { 50, 70, 50, 50 });
		renderer.setXLabelsColor(Color.WHITE);
		renderer.setYLabelsColor(0, Color.WHITE);

		setChartSettings(renderer, "",
				context.getString(R.string.abs_chart_title),
				context.getString(R.string.conc_chart_title), 0.000f, 4.000f,
				0.00f, 10.00f, Color.WHITE, Color.WHITE);
		renderer.setXLabels(6);
		renderer.setYLabels(5);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setYLabelsPadding(10);

		renderer.setZoomEnabled(false, false);

		renderer.setZoomEnabled(false);
		renderer.setClickEnabled(false);
		renderer.setPanEnabled(true, true);
		renderer.setLabelsTextSize(30);
		renderer.setPointSize(3);
		renderer.setShowLegend(false);

		XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);

		XYCombinedChartDef[] types = new XYCombinedChartDef[] {
				new XYCombinedChartDef(LineChart.TYPE, 0),
				new XYCombinedChartDef(ScatterChart.TYPE, 1) };
		return ChartFactory.getCombinedXYChartView(context, dataset, renderer,
				types);
	}

	@Override
	public Intent execute(Context context) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDesc() {
		// TODO Auto-generated method stub
		return null;
	}

}
